//
//  secondViewController.m
//  YLDragZoomCycleViewDemo
//
//  Created by 张雨露 on 2017/5/27.
//  Copyright © 2017年 张雨露. All rights reserved.
//

#import "YLSecondViewController.h"
#import "UIView+YLView.h"
#import "YLDragZoomCycleView.h"
#import "GKFadeNavigationController.h"
#import "Masonry.h"
#import "Xmg_Button.h"
#import "Xm_HotelRollView.h"
#import "UIBarButtonItem+XMGExtension.h"

@interface YLSecondViewController ()<UITableViewDelegate, UITableViewDataSource,GKFadeNavigationControllerDelegate,YLDroagViewDelegate>
@property(nonatomic, strong) YLDragZoomCycleView *dragView;
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic) GKFadeNavigationControllerNavigationBarVisibility navigationBarVisibility;


@property (nonatomic, strong) UIView *topheadView;

@property (nonatomic, strong) UIButton *searchButton;

/** 上一次点击的标题按钮 */
@property (nonatomic, weak) UIButton *previousClickedTitleButton;
@end

@implementation YLSecondViewController

#pragma mark -- macro

#define kHeaderHeight 200
#define NAVIGATION_HEIGHT (CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]) + CGRectGetHeight(self.navigationController.navigationBar.frame))

#pragma mark -- lazy init
- (UITableView *)tableView {
    if (!_tableView) {
        
        //判断数据个数，来确定tableView的高度
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 0, self.view.yl_width-10, self.view.yl_height) style:UITableViewStylePlain];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

#pragma mark -- life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
     
    self.title = @"Raindew";
    self.view.backgroundColor = [UIColor blackColor];
    [self setupUI];
    [self setupNavItems];
} 
- (void)setupNavItems
{ 
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem xmg_itemWithImage:@"左边" highTitle:@"" target:self action:@selector(onClickLeft)];
     
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem xmg_itemWithImage:@"右边" highTitle:@"" target:self action:@selector(onClickRight)];
    
    // 这里我就不适配了 (适配后 push/pop 时右边的黑影就会自动消失)
    self.searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 230, 30)];
    [self.searchButton setTitle:@"搜索职位/公司/商区" forState:UIControlStateNormal];
    self.searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.searchButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.searchButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    //[self.searchButton addTarget:self action:@selector(onClickSearchBtn) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = self.searchButton;
}

-(void)onClickRight{
    
    NSLog(@"右边");
}
-(void)onClickLeft{
    
    NSLog(@"👈左边");
    
}
#pragma mark -- set UI
- (void)setupUI {
    
    //添加表格
    [self.view insertSubview:self.tableView atIndex:0];
    
    //下面这行代码必须在这里设置。否则会影响导航栏颜色的更换。不能放在懒加载
    self.tableView.contentInset = UIEdgeInsetsMake(kHeaderHeight, 0, 0, 0);
    self.tableView.bouncesZoom=NO;
    
    _topheadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.yl_width, 270)];
    _topheadView.backgroundColor=[UIColor yellowColor];
    self.tableView.tableHeaderView=self.topheadView;
    
    
    
    // 头部试图View 层
    UIView *topLevelView = [[UIView alloc]initWithFrame:CGRectMake(10, -20, self.view.frame.size.width-20, 100)];
    topLevelView.backgroundColor=[UIColor orangeColor];
    [_topheadView addSubview:topLevelView];
    
    UIImageView *lineView = [[UIImageView alloc]init];
    lineView.backgroundColor=[UIColor grayColor];
    lineView.image=[UIImage imageNamed:@"centerLine"];
    [topLevelView addSubview:lineView];
    
    [lineView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.centerX.mas_equalTo(topLevelView.mas_centerX);
        //        make.centerY.mas_equalTo(topLevelView.mas_centerY);
        make.height.mas_offset(80);
        make.width.mas_offset(2);
    }];
    
    //邀请好友
    Xmg_Button *leftBtn = [[Xmg_Button alloc]initWithFrame:CGRectMake(CGRectGetMinX(topLevelView.frame)+30, 0, 100, 100)];
    [leftBtn setTitle:@"邀请好友" forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"mineHighlight"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(yaoqingTitleButtonLeftClick:) forControlEvents:UIControlEventTouchUpInside];
    [topLevelView addSubview:leftBtn];
    
    
    // 用户加盟
    Xmg_Button *rightBtn = [[Xmg_Button alloc]initWithFrame:CGRectMake(CGRectGetMaxX(topLevelView.frame)-150, 0, 100, 100)];
    [rightBtn setTitle:@"用户加盟" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(yonghuJiaMenTitleButtonLeftClick:) forControlEvents:UIControlEventTouchUpInside];
    [topLevelView addSubview:rightBtn];
    
    
    //  一间房一种生活
    Xm_HotelRollView *rollView=[[Xm_HotelRollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topLevelView.frame)+5,self.view.frame.size.width , 130)];
    
    [_topheadView addSubview:rollView];
    
    // 综合排序 距离优先 好评优先
    
    UIView *threebtnView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_topheadView.frame)-44, self.view.frame.size.width, 44)];
    threebtnView.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.11];
    [_topheadView addSubview:threebtnView];
    
    // 文字
    NSArray *titles = @[@"综合排序", @"距离优先",@"好评优先"];
    // 标题按钮的尺寸
    CGFloat titleButtonW = self.view.frame.size.width / titles.count;
    CGFloat titleButtonH = 44;
    
    // 创建5个标题按钮
    for (NSUInteger i = 0; i < titles.count; i++) {
        UIButton *titleButton = [[UIButton alloc] init];
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        titleButton.tag = i;
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [threebtnView addSubview:titleButton];
        // frame
        titleButton.frame = CGRectMake(i * titleButtonW, 0, titleButtonW, titleButtonH);
        // 文字
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
    }
    
    
    
    //创建轮播图
    self.dragView = [[YLDragZoomCycleView alloc] initWithFrame:CGRectMake(0, 0, self.view.yl_width, kHeaderHeight) andAutoScroll:YES scrollInterval:2];
    self.dragView.delegate = self;
    self.dragView.dataSource = [self getimageSource];
    [self.tableView insertSubview:self.dragView atIndex:0];
    
    //设置导航栏自动隐藏  下面的代码请放在方法的最后  否则影响导航栏判断
    
    //以下是控制导航栏透明效果的三方。推荐使用这个，或者更简单易用的WRNavigationBar
    GKFadeNavigationController *navigationController = (GKFadeNavigationController *)self.navigationController;
    [navigationController setNeedsNavigationBarVisibilityUpdateAnimated:NO];
    self.navigationBarVisibility = GKFadeNavigationControllerNavigationBarVisibilityHidden;

}



#pragma mark === 邀请好友
- (void)yaoqingTitleButtonLeftClick:(UIButton *)titleButton
{
    
    NSLog(@"-------邀请好友 -----------");
}
#pragma mark === 用户加盟
- (void)yonghuJiaMenTitleButtonLeftClick:(UIButton *)titleButton
{
    
    NSLog(@"-------用户加盟 -----------");
}


#pragma mark - 综合排序 按钮-- 监听
/**
 *  点击标题按钮
 */
- (void)titleButtonClick:(UIButton *)titleButton
{
    // 切换按钮状态
    self.previousClickedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.previousClickedTitleButton = titleButton;
    
}

- (void)didSelectedItem:(NSInteger)item {
    NSLog(@"选择了第%ld张图片",item+1);
}

#pragma mark -- UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行\t上下滑动试试~",indexPath.row];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat scrollOffsetY = kHeaderHeight-scrollView.contentOffset.y;
    // Show or hide the navigaiton bar
    if (scrollOffsetY-kHeaderHeight < NAVIGATION_HEIGHT) {
        self.navigationBarVisibility = GKFadeNavigationControllerNavigationBarVisibilitySystem;//系统默认展示
    }else {
        self.navigationBarVisibility = GKFadeNavigationControllerNavigationBarVisibilityHidden;//自定义bar隐藏
    }

    CGFloat newOffsetY = scrollView.contentOffset.y;
    if (newOffsetY < - kHeaderHeight) {
        self.dragView.frame = CGRectMake(0, newOffsetY, self.view.frame.size.width, -newOffsetY);
    }
}
//正在拖拽的时候停止自动滚动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.dragView stopScroll];
}
//停止滑动开启自动滚动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self.dragView startScroll];
}


#pragma mark - Accessors

- (void)setNavigationBarVisibility:(GKFadeNavigationControllerNavigationBarVisibility)navigationBarVisibility {
    
    if (_navigationBarVisibility != navigationBarVisibility) {
        // Set the value
        _navigationBarVisibility = navigationBarVisibility;
        // Play the change
        GKFadeNavigationController *navigationController = (GKFadeNavigationController *)self.navigationController;
        if (navigationController.topViewController) {
            [navigationController setNeedsNavigationBarVisibilityUpdateAnimated:YES];
        }
    }
}

#pragma mark <GKFadeNavigationControllerDelegate>

- (GKFadeNavigationControllerNavigationBarVisibility)preferredNavigationBarVisibility {
    return self.navigationBarVisibility;
}

- (void)dealloc {
    NSLog(@"Raindew控制器销毁了");
}

- (NSArray *)getimageSource {

    return @[@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1495880401999&di=ba4990f6e6d89d66dcea9832ea01c217&imgtype=0&src=http%3A%2F%2Fimg1.3lian.com%2F2015%2Fa1%2F78%2Fd%2F250.jpg",
    @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510911940541&di=f682473e09a88e6a4003793ef23531ee&imgtype=0&src=http%3A%2F%2Fimg1.3lian.com%2F2015%2Fa1%2F91%2Fd%2F14.jpg",
    @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510911989592&di=11c2d75849c4e77b8d8be6651a029563&imgtype=0&src=http%3A%2F%2Fimg06.tooopen.com%2Fimages%2F20160914%2Ftooopen_sy_178863864613.jpg",
    @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1495880548256&di=66722048d74180758bff742f9855caf4&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2015%2F162%2F48%2F9TZ0JJK73519.jpg"];

}

@end
